package com.ics.cmsadmin.modules.basic.service.impl;

import com.ics.cmsadmin.frame.core.bean.LoginSearchBean;
import com.ics.cmsadmin.frame.core.bean.PageBean;
import com.ics.cmsadmin.frame.core.bean.PageResult;
import com.ics.cmsadmin.frame.core.enums.OrderStatusEnum;
import com.ics.cmsadmin.frame.utils.KeyUtil;
import com.ics.cmsadmin.modules.auth.service.AccessService;
import com.ics.cmsadmin.modules.basic.bean.OrderBean;
import com.ics.cmsadmin.modules.basic.dao.OrderDao;
import com.ics.cmsadmin.modules.basic.service.OrderService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * t_order
 * Created bylvsw on 2018-16-23 21:09:34.
 */
@Service("newOrderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private AccessService accessService;

    @Override
    public PageResult list(OrderBean bean, PageBean page){
        long count = orderDao.count(bean);
        if (count == 0){
            return new PageResult();
        }
        page = page == null ? new PageBean() : page;
        return PageResult.getPage(count, orderDao.list(bean, page));
    }

    @Override
    public PageResult listByLoginUserId(OrderBean bean, String loginUserId, PageBean page) {
        return listByLoginUserId(bean, loginUserId, page, orderDao);
    }

    @Override
    public boolean insert(OrderBean bean) {
        if (bean == null){
            return false;
        }
        bean.setId(KeyUtil.genUniqueKey());
        return orderDao.insert(bean) == 1;
    }

    @Override
    public boolean update(OrderBean bean) {
        return orderDao.update(bean) == 1;
    }

    @Override
    public OrderBean queryUserOrder(String userId, String courseId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        List<OrderBean> orderList = orderDao.list(OrderBean.builder().userId(userId).courseId(courseId).build(),
            new PageBean(1, Integer.MAX_VALUE));
        return !CollectionUtils.isEmpty(orderList) ? orderList.get(0) : null;
    }

    @Override
    public boolean getPurchaseStatus(String userId, String courseId) {
        OrderBean orderBean = queryUserOrder(userId, courseId);
        return orderBean != null && orderBean.getStatus().equals(OrderStatusEnum.SUCCESS.getCode());
    }

    @Override
    public long count(OrderBean t) {
        return orderDao.count(t);
    }

    @Override
    public void batchDelete(ArrayList<String> ids) {
        orderDao.batchDelete(ids);
    }

    @Override
    public void batchSoftRemove(ArrayList<String> ids) {
        orderDao.batchSoftRemove(ids);
    }


    @Override
    public BigDecimal totalMoney(OrderBean bean, String loginUserId) {
        LoginSearchBean loginSearchBean = accessService.queryLoginInfoByLoginUserId(loginUserId);
        if (loginSearchBean == null){
            return new BigDecimal(0);
        }
        bean.setLoginAgentNo(loginSearchBean.getLoginAgentNo());
        bean.setLoginOrgId(loginSearchBean.getLoginOrgId());
        return orderDao.totalMoney(bean);
    }

    @Override
    public OrderBean queryById(String id) {
        return orderDao.queryById(id);
    }

}
